//引入公共样式
require("../../assets/js/commonCss");
require("./data.less"); //引入自己的样式

// 导入axios
const axios = require("../../utils/request");
// 设置服务器地址
axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";


// 导入weui
require("weui"); //导入weui的css
let weui = require("weui.js"); //导入weui的js

// 导入echarts
const echarts = require("echarts"); //nodejs的导入

// 返回按钮
document.querySelector("#backBtn").addEventListener("click", function() {
    history.back();
});

axios.get("/api/user/info").then(function(res) {
    console.log(res.data);
    if (res.data.errno === 0) {
        let data = res.data.data;
        // 头像
        document.querySelector("#imgUrl").src =
            axios.defaults.baseURL + data.imgUrl;
        //会员累计打卡数量

        // 卡路里
        document.querySelector("#calorie").textContent = data.calorie;
        // 时长
        document.querySelector("#duration").textContent = data.duration;
    } else if (res.data.errno === 10003) {
        weui.alert("未登录,请重新登录!", function() {
            location.href = "./login.html";
        });
    }
});

// 初始化一个echarts对象
let myCharts = echarts.init(document.querySelector("#e1"));
// 设置图表的配置项
let option = {
    // x轴
    xAxis: {
        type: "category",
        data: ["9.11", "9.12", "9.13", "9.14", "9.15", "9.16", "9.17"],
    },
    // y轴
    yAxis: {
        type: "value",
    },
    // 核心数据
    series: [{
        data: [120, 200, 150, 80, 70, 110, 130],
        type: "bar",
    }, ],
};

// 最后一步
myCharts.setOption(option);

//给id="e1" 柱状图
let x = ["9/1", "9/2", "9/3", "9/4", "9/5", "9/6", "9/7"];
let y = [120, 300, 150, 80, 70, 110, 130];
sportTime(x, y);

function sportTime(arr1, arr2) {
    //1.初始化echarts对象
    let myCharts = echarts.init(document.querySelector("#e2"));
    //2.设置配置选项---官网的示例中心 乾坤大挪移
    let option = {
        //标题
        title: {
            text: "近7天运动时长",
            textStyle: {
                color: "#000",
                fontSize: 14,
            },
            // left:"center" 水平位置
            // top:"center", 垂直位置
        },
        //颜色
        color: ["#83A7D1"],
        //x轴
        xAxis: {
            type: "category",
            data: arr1,
        },
        //y轴
        yAxis: {
            type: "value",
        },
        //核心数据
        series: [{
            data: arr2,
            type: "bar",
        }, ],
    };
    //3.画图
    myCharts.setOption(option);
}

//运动分类 饼状图
let cate = [{
        value: 250,
        name: "跑步",
    },
    {
        value: 350,
        name: "骑行",
    },
    {
        value: 400,
        name: "训练",
    },
];
sportCate(cate);

function sportCate(data) {
    //1.初始化echarts对象
    let myCharts = echarts.init(document.querySelector("#e3"));
    //2.设置配置选项---官网的示例中心 乾坤大挪移
    let option = {
        //标题
        title: {
            text: "运动分类",
            left: "left",
            textStyle: {
                color: "#333",
                fontSize: 14,
            },
        },
        //提示框
        tooltip: {
            trigger: "item",
        },
        //图例
        legend: {
            orient: "vertical",
            left: "left",
            top: "center",
            icon: "rect",
            itemWidth: 10,
            itemHeight: 10,
        },
        color: ["#58BB92", "#736171", "#EE5C43"],
        //核心数据
        series: [{
            name: "Access From",
            type: "pie",
            radius: "50%",
            data: data, //后端给你
            label: {
                position: "inside", //在里面显示
                color: "#fff",
                formatter: "{d}%", //百分比
            },
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: "rgba(0, 0, 0, 0.5)",
                },
            },
        }, ],
    };
    //3.画图
    myCharts.setOption(option);
}